#!/usr/bin/env python

import os
import subprocess

checkout_command = ['git', 'clone',
                    'git://fontforge.git.sourceforge.net/gitroot/fontforge/fontforge']
reset_command = ['git', 'reset', 'da172a14d38666ded543685391c8b05f2f71e786']

patch_files = [
    (1, os.path.join(os.path.abspath('fontforge-patch'), 'python3.2011.06.14.20.45.14.patch')),
]

def patch_command(level):
    return ['patch', '-Np' + str(level)]

if os.path.isdir('fontforge'):
    print('')
    print('You already have a "fontforge" subdirectory.')
    print('Due to lack of programmer initiative, you will have')
    print('to figure out for yourself what to do about it.')
    print('I\'m giving up. :)')
    print('')
    exit(1)

else:
    p = subprocess.Popen(checkout_command)
    os.waitpid(p.pid, 0)[1]

    os.chdir('fontforge')

    p = subprocess.Popen(reset_command)
    os.waitpid(p.pid, 0)[1]

    for (level, file_name) in patch_files:
        with open(file_name) as f:
            p = subprocess.Popen(patch_command(level), stdin = f)
            os.waitpid(p.pid, 0)[1]
